//
//  RocBluetoothVC.m
//  RocRemoteControlProject
//
//  Created by 新致李照鹏 on 2018/12/25.
//  Copyright © 2018年 Roc. All rights reserved.
//

#import "RocBluetoothVC.h"
#import "RocBluetoothView.h"

@interface RocBluetoothVC () <RocBluetoothViewDelegate>

@property (nonatomic, strong) RocBluetoothView *myView;

@end

@implementation RocBluetoothVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self basicSet];
    
    [self buildMainUI];
    
    [self StartBluetooth];
}
//支持旋转
-(BOOL)shouldAutorotate{
    return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
//一开始的方向  很重要
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}
#pragma mark - 基础设置
- (void)basicSet
{
    self.title = @"绑定设备";
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark - 蓝牙相关
/**
  启动蓝牙
 */
- (void)StartBluetooth
{
    __weak typeof(self)weakSelf = self;
    if ([RocBluetoothManager Manager].bluetooth_isOpen) {
        [self.myView showWithState:RocBluetoothViewStateOpen];
    }else{
        [self.myView showWithState:RocBluetoothViewStateNotOpen];
        [[RocBluetoothManager Manager] startMonitorBluetoothStatus:^(BOOL isOpen, NSString * _Nonnull tips) {
            if (isOpen) {
               [weakSelf.myView showWithState:RocBluetoothViewStateOpen];
            }
        }];
    }
    
}
/**
  启动扫描
 */
- (void)blueStartScan
{
    __weak typeof(self)weakSelf = self;
    [[RocBluetoothManager Manager] startScanDevice:^(NSString * _Nonnull deviveName) {
        if (weakSelf.myView.currentState != RocBluetoothViewStateGet) {
            [weakSelf.myView showWithState:RocBluetoothViewStateGet];
        }
        if (deviveName == nil) {
            deviveName = @"null";
        }
        [weakSelf.myView setupDeviveName:deviveName];
    }];
}

#pragma mark - 搭建界面
- (void)buildMainUI
{
    [self buildLeftBarButton];
    
    [self buildView];
}
// 返回按钮
- (void)buildLeftBarButton
{
    UIImage *image = [UIImage imageNamed:@"backarr.png"];
    
    UIButton *button = [[UIButton alloc] init];
    [button setImage:image forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    button.imageEdgeInsets = UIEdgeInsetsMake(8, -8, 8, 20);
    [button addTarget:self action:@selector(leftBarButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]  initWithCustomView:button];
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(leftBarButtonAction)];
}
- (void)leftBarButtonAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)buildView
{
    RocBluetoothView *view = [[RocBluetoothView alloc] initWithFrame:self.view.bounds];
    view.mydelegate = self;
    [self.view addSubview:view];
    
    self.myView = view;
}

- (void)RocBluetoothView:(RocBluetoothView *)view ClickButton:(RocBluetoothViewButtonType)type
{
    if (type == RocBluetoothViewButtonOpen) {
        [self JudgeToSetting];
    }else if (type == RocBluetoothViewButtonScan){
        [view showWithState:RocBluetoothViewStateScaning];
        [self blueStartScan];
    }
}

- (void)JudgeToSetting
{
    NSURL *url = [NSURL URLWithString:@"App-Prefs:root=Bluetooth"];
    if ([[UIApplication sharedApplication]canOpenURL:url]) {
        [[UIApplication sharedApplication]openURL:url];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
